﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManager
{
    public partial class Admin2 : Form
    {
        public Admin2()
        {
            InitializeComponent();
            
        }

        private void Admin2_Load(object sender, EventArgs e)
        {
            Table();
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }

        //从数据库读取数据显示在表格中
        public void Table()
        {
            //清空旧数据
            dataGridView1.Rows.Clear();
            Dao dao = new Dao();
            string sql = "select * from t_books";
            IDataReader dc = dao.read(sql);

            while(dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            { 
                //书号
                string id  = dataGridView1.Rows[0].Cells[0].Value.ToString();
                //书号 + 书名
                label2.Text = id + dataGridView1.Rows[0].Cells[1 ].Value.ToString();
                DialogResult dr = MessageBox.Show("确认删除么","信息提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
                if (dr == DialogResult.OK) 
                {
                    string sql = $"delete from t_books where id = '{id}'";
                    Dao dao = new Dao();
                    if (dao.Execute(sql) > 0)
                    {
                        MessageBox.Show("删除成功");
                        //刷新
                        Table();
                    }
                    else
                    {
                        MessageBox.Show("删除失败" +  sql);
                    }
                    dao.DaoClose();

                }
            }
            catch
            {
                MessageBox.Show("请先在表格选中要删除的图书记录！", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            admin21 a = new admin21();
            //this.Hide();
            a.ShowDialog();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                string author = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                string publish = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                string amount = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                admin22 admin = new admin22(id, name, author, publish, amount);
                admin.ShowDialog();
                Table();
            }
            catch
            {
                MessageBox.Show("Error");
            }            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            TableID();
        }


        //根据ID显示数据
        public void TableID()
        {
            //清空旧数据
            dataGridView1.Rows.Clear();
            Dao dao = new Dao();
            string sql = $"select * from t_books where id = '{textBox1.Text}'";
            IDataReader dc = dao.read(sql);

            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        //根据书名显示数据
        public void TableName()
        {
            //清空旧数据
            dataGridView1.Rows.Clear();
            Dao dao = new Dao();
            string sql = $"select * from t_books where name like  '%{textBox2.Text}%'";
            IDataReader dc = dao.read(sql);

            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            TableName();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Table();
            textBox1.Text = "";
            textBox2.Text = "";
        }
    }
}
